<template>
	<view class="zai-box">
		<titleBar title="登录" textColor="#fff" isReturn=true textAlign="center"></titleBar>
		<scroll-view scroll-y class="page">

			<view style="text-align: center;" :style="[{animation: 'show ' + 0.4+ 's 1'}]">
				<image src="/static/logo.gif" mode='aspectFit' class="zai-logo"></image>
			</view>
			<view class="box padding-lr-xl login-paddingtop" :style="[{animation: 'show ' + 0.6+ 's 1'}]">

				<view class="cu-form-group margin-top round shadow-blur bg-gray ">
					<view class="title">账号:</view>
					<input placeholder="admin" name="input" v-model="userName"></input>
				</view>
				<view class="cu-form-group margin-top round bg-gray">
					<view class="title">密码:</view>
					<input placeholder="admin" name="input" type="password" v-model="password"></input>
				</view>
				<view class=" margin-top round bg-white " style="width: 300rpx; margin-left: 400rpx;">
					<view class="title text-sm text-gray">记住密码:
						<switch class='margin-left radius' @change="Switch" :class="switchType?'checked':''"
							:checked="switchType?true:false"></switch>
					</view>
				</view>
				<view v-if="!updating" class="padding  flex  flex-direction">
					<button class="cu-btn bg-green shadow-blur round lg" :loading="loading" @tap="onLogin">
						{{loading ? "登录中...":"登 录"}}
					</button>
				</view>
				<!-- #ifdef APP-PLUS -->
				<view v-if="!updating" class="padding flex flex-direction  text-center  ">
					当前版本:{{version}}
				</view>
				<view v-if="!updating" class=" flex flex-direction  text-center text-xs text-gray ">
					{{versionDetil}}
				</view>
				<!--下载进度条-->
				<view v-if="updating" class="padding bg-white text-grey">
					更新中...
					<view class="cu-progress round sm striped active">
						<view class="bg-green" :style="[{ width:updating?updatingTime:''}]">{{updatingTime}}</view>
					</view>

				</view>
				<!-- #endif -->

			</view>
		</scroll-view>
		<!-- 登录加载弹窗 -->
		<view class="cu-load load-modal" v-if="loading">
			<!-- <view class="cuIcon-emojifill text-orange"></view> -->
			<image src="/static//logo.gif" mode="aspectFit"></image>
			<view class="gray-text">登录中...</view>
		</view>
		<!-- <my-image-upload></my-image-upload>
		<my-select></my-select> -->
		<!-- <my-page></my-page> -->

		<!-- 模态弹出提示未进出场 -->
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">尚未进场项目</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>

				<view class="padding-xl">
					<view class="text-sl padding">
						<text class=" cuIcon-warnfill text-red"></text>
					</view>
					<view class="padding">您尚未入场任何项目及合同段，请联系管理员进行人员进场。</view>
				</view>
			</view>
		</view>

	</view>

</template>

<script>
	// import { ACCESS_TOKEN,USER_NAME,USER_INFO } from "@/common/util/constants"
	// import { mapActions } from "vuex"
	// import myImageUpload from "@/components/my-componets/my-image-upload.vue"
	// import mypage from "@/components/my-componets/my-page.vue"
	// import myselect from "@/components/my-componets/my-select.vue"
	import titleBar from '@/components/titleBar/titleBar.vue'
	export default {
		components: {
			titleBar,
			// 'my-image-upload':myImageUpload,
			// 'my-select':myselect
		},
		data() {
			return {
				switchType: '',
				loading: false,
				updating: false,
				updatingTime: '',
				userName: '',
				password: '',
				phoneNo: '',
				smsCode: '',
				showPassword: false, //是否显示明文
				loginWay: 1, //1: 账密，2：验证码
				smsCountDown: 0,
				smsCountInterval: null,
				toggleDelay: false,
				version: '',
				versionDetil: '',
				updateApp: '/biz/bizApp/onlineUpgrade',
				modalName: null,
			};
		},

		onLoad: function() {
			this.switchType = uni.getStorageSync('switchType')

			if (this.switchType) {
				let loginParams = uni.getStorageSync('loginParams')
				this.userName = loginParams.username
				this.password = loginParams.password
			}
			// #ifdef APP-PLUS

			var that = this
			plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
				that.version = wgtinfo.version
				uni.setStorageSync('version', that.version)
			});
			// #endif

			setTimeout(() => {
				// this.update()
			}, 500)

		},
		methods: {
			hideModal() {
				this.modalName = null
			},
			update() {
				//获取当前版本号
				let version = uni.getStorageSync('version')

				//请求服务端下载接口，返回一个下载链接
				this.$http.get(this.updateApp, {
					params: {
						currentVersion: version
					}
				}).then(res => {

					if (res.data.success) {

						if (!res.data.result.appUrl) {

							this.versionDetil = res.data.result

						} else {
							let downLoadUrl = res.data.result.appUrl
							uni.showModal({
								title: '版本更新',
								content: '确定要更新吗？',
								cancelText: '取消',
								confirmText: '确定',
								success: res => {
									if (res.confirm) {

										//启动下载进度条
										this.updating = true

										//使用下载链接下载app
										const downloadTask = uni.downloadFile({


											url: downLoadUrl,

											success: (downloadResult) => {

												if (downloadResult.statusCode === 200) {

													uni.showToast({
														title: '安装包下载成功，即将安装',
														icon: 'none'
													})

													this.updating = false

													setTimeout(() => {
														plus.runtime.openFile(
															downloadResult
															.tempFilePath);
													}, 1000)

												} else {
													this.updating = false
													uni.showToast({
														title: '更新失败',
														mask: false,
														duration: 1500
													});
												}


											},
											complete: (res) => {
												console.log(res)
											}
										});

										downloadTask.onProgressUpdate((res) => {
											this.updatingTime = res.progress + '%'

										});

									}
								}
							})
						}
					}

				}).catch(err => {
					console.log(err);
				});

			},
			Switch(e) {
				this.switchType = e.detail.value

			},
			// ...mapActions([ "mLogin","PhoneLogin" ]),

			onLogin: function() {

				if (!this.userName || this.userName.length == 0) {
					uni.showToast({
						title: '请填写用户名',
						icon: 'none',
						duration: 2000
					});

					// this.$tip.toast('请填写用户名');
					return;
				}
				if (!this.password || this.password.length == 0) {
					uni.showToast({
						title: '请填写密码',
						icon: 'none',
						duration: 2000
					});
					// this.$tip.toast('请填写密码');
					return;
				}
				let loginParams = {
					username: this.userName,
					password: this.password
				}
				uni.setStorageSync('loginParams', loginParams)
				uni.setStorageSync('switchType', this.switchType)
				this.loading = true;


				// debugger
				var that = this

				uni.request({

					url: "https://www.turkeylike.cn:8900/fzulab/sys/mLogin",
					method: 'post',
					data: {
						userName: this.userName,
						password: this.password,

					},


					success(res) {
						if (res.data.code == '500') {
							uni.showToast({
								title: '账号密码不正确',
								icon: 'none',
								duration: 2000
							});
							that.loading = false;
							return
						}
						
						uni.setStorageSync('isVisitor', false)
						uni.setStorageSync('userId', res.data.result.userId)
						uni.setStorageSync('avatar', res.data.result.avatar)
						uni.setStorageSync('name', res.data.result.name)
						uni.setStorageSync('userName', res.data.result.username)
						uni.setStorageSync('token', res.data.result.token)
						console.log(uni.getStorageSync("isVisitor"))
						console.log(uni.getStorageSync("userId"))
						console.log(uni.getStorageSync("token"))
						uni.switchTab({
							url: '/pages/home/home',
							success: function(e) {
								// pop() 方法用于删除数组其他元素并返回数组的最后一个元素
								let currentPage = getCurrentPages().pop(); //当前页面
								if (currentPage == undefined || currentPage == null) return;
								currentPage.onLoad(); //页面存在刷新当前页面
								//currentPage.onShow()
								//currentPage.onReady()
								// 或者执行其他生命周期刷新
							},
							fail: (e) => {
								
								console.log("getMachineNum fail:" + JSON.stringify(e));
							},
							
						});

						// wx.switchTab({
						// 	//跳转地址
						// 	url: '/pages/home/home'
						// })
						// let urlList = res.data.result.list
						// // debugger 
						// console.log(urlList)				
						// for (var i = 0; i < urlList.length; i++) {
						// 	that.announcementList.push({
						// 		'title': urlList[i].title,
						// 		'createDate': urlList[i].createDate,

						// 	})
						// }

					}
				})

				// console.log(that.announcementList.length)



				//       this.mLogin(loginParams).then((res) => {

				//   this.loading=false;
				//         if(res.data.success){
				// 	  uni.setStorageSync('userId',res.data.result.userInfo.id)
				// 	  uni.setStorageSync('userInfo',res.data.result.userInfo)
				// 	  if(res.data.result.userInfo.isEnterProject==0){
				// 		 this.modalName = 'Modal'

				// 	  }else{

				// 		  this.$tip.success('登录成功!')
				// 		  this.$Router.replaceAll({name:'index'})
				// 	  }


				//         }else{
				//             this.$tip.alert1(res.data.message);
				//         }
				//       }).catch((err) => {
				//         let msg = err.data.message || "请求出现错误，请稍后再试"
				//         this.$tip.alert(msg);
				//       }).finally(()=>{
				//   this.loading=false;
				// })
			}
		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}

	.login-paddingtop {
		padding-top: 200 upx;
	}

	.zai-box {
		padding: 0 20 upx;
		padding-top: 100 upx;
		position: relative;

	}

	.zai-logo {
		width: 450rpx;
		height: 300 rpx;
	}

	.zai-title {
		font-size: 58upx;
		color: #000000;
		text-align: center;
	}

	.input-placeholder,
	.zai-input {
		color: #94afce;
	}

	.zai-label {
		padding: 60 upx 0;
		text-align: center;
		font-size: 30 upx;
		color: #a7b6d0;
	}

	.zai-btn {
		background: #ff65a3;
		color: #fff;
		border: 0;
		border-radius: 100 upx;
		font-size: 36 upx;
	}

	.zai-btn:after {
		border: 0;
	}

	/*按钮点击效果*/
	.zai-btn.button-hover {
		transform: translate(1 upx, 1 upx);
	}
</style>
